<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>
    // 冒泡排序
    var arr = [9, 2, 7, 3, 1, 5, 6, 8, 4];
    //升序:两两相比较,如果前者值比较大,两值位置互换
    for (let i = 0; i < arr.length - 1; i++) {
        for (let j = 0; j < arr.length - 1 - i; j++) {
            if (arr[j] > arr[j + 1]) {
                /* var a = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = a; */
                [arr[j],arr[j + 1]]=[arr[j + 1],arr[j]];
            }
        }
    }
    document.write(arr+'<br>');

    //选择排序:第一次拿最左侧的元素依次向右比较,记录最大或最小的值
    var minnum;
    var arr1 = [9, 2, 7, 3, 1, 5, 6, 8, 4];
    for (let i = 0; i < arr1.length-1; i++) {
        minnum=i;
        for (let j = i; j < arr1.length; j++) {
            if (arr1[j+1]<arr1[minnum]) {
                minnum=j+1;
            }
        }
        [arr1[i],arr1[minnum]]=[arr1[minnum],arr1[i]];
    }
    document.write(arr1);

    // 插入排序:把一个数据插入到已经排好的数组中
    /* var a=4;
    var arr1=[1,2,3,4,5];
    for (let i = 0; i < arr.length; i++){
        if (arr[i]) {
            
        }
    } */

</script>

</html>